package cn.edu.scnu.semart.goods.controller;

import cn.edu.scnu.semart.model.goods.Comment;
import cn.edu.scnu.semart.goods.service.CommentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 商品评论Controller
 */
@RestController
@RequestMapping("/comment")
//@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     */
    @PostMapping("/add")
    public Map<String, Object> addComment(@RequestBody Comment comment) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = commentService.addComment(comment);
            result.put("success", success);
            if (success) {
                result.put("message", "评论添加成功");
            } else {
                result.put("message", "评论添加失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 根据商品ID获取评论列表
     */
    @GetMapping("/goods/{goodsId}")
    public Map<String, Object> getCommentsByGoodsId(
            @PathVariable Integer goodsId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            Page<Comment> page = new Page<>(current, size);
            IPage<Comment> commentPage = commentService.getCommentsByGoodsId(goodsId, page);
            
            result.put("success", true);
            result.put("data", commentPage);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 根据用户ID获取评论列表
     */
    @GetMapping("/user/{userId}")
    public Map<String, Object> getCommentsByUserId(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {

        Map<String, Object> result = new HashMap<>();
        try {
            Page<Comment> page = new Page<>(current, size);
            IPage<Comment> commentPage = commentService.getCommentsByUserId(userId, page);

            result.put("success", true);
            result.put("data", commentPage);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> deleteComment(
            @PathVariable Integer id,
            @RequestParam Integer userId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = commentService.deleteComment(id, userId);
            result.put("success", success);
            if (success) {
                result.put("message", "评论删除成功");
            } else {
                result.put("message", "评论删除失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 点赞评论
     */
    @PostMapping("/like/{commentId}")
    public Map<String, Object> likeComment(
            @PathVariable Integer commentId,
            @RequestParam Integer userId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = commentService.likeComment(commentId, userId);
            result.put("success", success);
            if (success) {
                result.put("message", "点赞成功");
            } else {
                result.put("message", "点赞失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 取消点赞评论
     */
    @PostMapping("/unlike/{commentId}")
    public Map<String, Object> unlikeComment(
            @PathVariable Integer commentId,
            @RequestParam Integer userId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = commentService.unlikeComment(commentId, userId);
            result.put("success", success);
            if (success) {
                result.put("message", "取消点赞成功");
            } else {
                result.put("message", "取消点赞失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取用户收到的评价列表
     */
    @GetMapping("/received/{userId}")
    public Map<String, Object> getReceivedComments(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {

        Map<String, Object> result = new HashMap<>();
        try {
            Page<Comment> page = new Page<>(current, size);
            IPage<Comment> commentPage = commentService.getReceivedComments(userId, page);

            result.put("success", true);
            result.put("data", commentPage);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取用户收到的平均评分
     */
    @GetMapping("/average-rating/{userId}")
    public Map<String, Object> getUserAverageRating(@PathVariable Integer userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Double averageRating = commentService.getUserAverageRating(userId);
            result.put("success", true);
            result.put("data", averageRating);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取商品评论统计
     */
    @GetMapping("/statistics/{goodsId}")
    public Map<String, Object> getCommentStatistics(@PathVariable Integer goodsId) {
        Map<String, Object> result = new HashMap<>();
        try {
            CommentService.CommentStatistics statistics = commentService.getCommentStatistics(goodsId);
            result.put("success", true);
            result.put("data", statistics);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
} 